import { Component } from 'react';

const WithFetch = (url) => (View) => {
  return class extends Component {
    constructor () {
      super();
      this.state = {
        loading: true,
        data: null,
      }
    }
    componentDidMount () {
      fetch(url)
      .then(res => res.json())
      .then(data => {
        this.setState({
          loading: false,
          data: data
        })
      })
    }
    render () {
      if (this.state.loading) {
        return (
          <div>
            Loading...
          </div>
        )
      } else {
        return <View data={this.state.data}></View>
      }
    }
  }
}

export default WithFetch;